package com.example.shellshopping.personCenter;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shellshopping.R;
import com.example.shellshopping.login.PersonCenterMainActivity;
import com.example.shellshopping.login.SettingGetQuestion;
import com.example.shellshopping.login.UserBean;
import com.example.shellshopping.utils.BaseActivity;
import com.example.shellshopping.utils.SaveUserUtils;
import com.example.shellshopping.utils.URLUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class PersonSettingQuestionActivity extends BaseActivity {

    TextView question_tv;
    EditText answer_et;
    Button next_btn;
    ImageView back_TV;
    //SettingGetQuestion set = null;

    private UserBean.DataBean userInfo; //之前登录时保存的用户信息
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_setting_question);
        userInfo = SaveUserUtils.getUserInfo(this);

        initView();
        question_tv.setText(PersonCenterSettingActivity.str);
        setListener();
        //set.networkLink();
    }

    private void setListener() {
        next_btn.setOnClickListener(onClickListener);
        back_TV.setOnClickListener(onClickListener);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.person_setting_question_btn:
                    //判断密保问题是否正确
                    isQuestion();
                    break;
                case R.id.person_setting_question_iv_back:
                    //判断密保问题是否正确
                    backLast();
                    break;
            }
        }
    };

    private void isQuestion() {
        String answer = answer_et.getText().toString();
        boolean flag = judgeInput(answer);  //判断密保答案是否为空
        if (flag) {  //可以执行网络请求
            Map<String,String> map = new HashMap<>();
            map.put("id", String.valueOf(userInfo.getId()));
            map.put("answer",answer);
            postNetwork(URLUtils.verifyAnswer_url,map);
        }
    }

    private boolean judgeInput(String answer) {
        if (TextUtils.isEmpty(answer)) {
            Toast.makeText(this,"输入内容不能为空！",Toast.LENGTH_SHORT).show();
            return false;
        }else return true;
    }

    @Override
    public void onSuccess(String result){
        super.onSuccess(result);
        //解析数据
        UserBean bean = new Gson().fromJson(result,UserBean.class);
        //
        int code = bean.getCode();
        switch(code){
            case 200:  //登录成功
                jumpToRepwd();
                break;
            case 201:  //账号或密码错误
                break;
            default:   //其他，登录失败
                break;
        }
    }

    private void jumpToRepwd() {
        finish();
        Intent intent = new Intent(this, PersonSettingRepwdActivity.class);
        startActivity(intent);
    }

    private void backLast() {
        finish();
    }

    private void initView() {
        question_tv = findViewById(R.id.person_setting_question_tv);
        answer_et = findViewById(R.id.person_setting_question_et);
        next_btn = findViewById(R.id.person_setting_question_btn);
        back_TV = findViewById(R.id.person_setting_question_iv_back);
    }
}